﻿using WpfModelEditor.Core.Plugins;
using AvalonDock.Layout;
using Fluent;
using System.Diagnostics;
using MahApps.Metro.IconPacks;

namespace ViewPlugin
{
    public class ViewPluginImpl : IPlugin
    {
        private NodePanel _nodePanel;
        private PropertyPanel _propertyPanel;
        private LayoutAnchorable _properties;
        private MaterialPanel _materialPanel;
        private AnimationPanel _animationPanel;
        private LayoutAnchorable _nodes;
        private LayoutAnchorable _materials;
        private LayoutAnchorable _animations;
        private static ViewPluginImpl _instance = null;
        public static ViewPluginImpl Instance { get { return _instance; } }
        public ViewPluginImpl()
        {
            Debug.Assert(_instance == null);
            _instance = this;
        }
        public int Order { get { return 30; } }
        public IHost Host { get; private set; }
        public void Initialize(IHost host)
        {
            Host = host;
            var tab = host.UI.GetOrCreateRibbonTabItem("视图");
            var group = new RibbonGroupBox
            {
                Header = "显示",
            };
            tab.Groups.Add(group);
            InitShow(group);

            group = new RibbonGroupBox
            {
                Header = "视口",
            };
            tab.Groups.Add(group);

            {
                var btn = new Fluent.Button
                {
                    Header = "重置相机",
                    SizeDefinition = RibbonControlSizeDefinition.FromString("Large"),
                    LargeIcon = new PackIconMaterial { Kind = PackIconMaterialKind.LockReset },
                    Icon = new PackIconMaterial { Kind = PackIconMaterialKind.LockReset },
                };
                btn.Click += (s, e) => Host.FocusCameraToScene();
                group.Items.Add(btn);
            }

            group = new RibbonGroupBox
            {
                Header = "动画",
            };
            tab.Groups.Add(group);
            {
                var btn = new Fluent.Button
                {
                    Header = "停止",
                    LargeIcon = new PackIconMaterial { Kind = PackIconMaterialKind.Stop },
                    Icon = new PackIconMaterial { Kind = PackIconMaterialKind.Stop },
                };
                btn.Click += (s, e) => Host.StopAnimation();
                group.Items.Add(btn);
                var spinner = new Fluent.Spinner
                {
                    Minimum = 0.1,
                    Maximum = 10,
                    Value = 1.0
                };
                group.Items.Add(spinner);
                var combo = new Fluent.ComboBox
                {
                    IsEditable = false,
                };
                group.Items.Add(combo);
            }
        }
        private void InitShow(RibbonGroupBox group)
        {
            var showSkybox = new Fluent.CheckBox
            {
                Header = "环境贴图",
                IsChecked = true,
            };
            showSkybox.Click += (s, e) => { Host.RenderEnvironmentMap = showSkybox.IsChecked.Value; };
            showSkybox.IsChecked = Host.RenderEnvironmentMap;
            if (Host.EnvironmentMap.Texture == null)
                showSkybox.IsEnabled = false;
            group.Items.Add(showSkybox);

            var showAxis = new Fluent.CheckBox { Header = "坐标轴", IsChecked = true };
            showAxis.Click += (s, e) => { Host.ActiveView.ShowCoordinateSystem = showAxis.IsChecked.Value; };
            showAxis.IsChecked = true;
            Host.ActiveView.ShowCoordinateSystem = true;
            group.Items.Add(showAxis);
            var showCube = new Fluent.CheckBox { Header = "视立方体", IsChecked = true };
            showCube.Click += (s, e) => { Host.ActiveView.ShowViewCube = showCube.IsChecked.Value; };
            showCube.IsChecked = true;
            Host.ActiveView.ShowViewCube = true;
            group.Items.Add(showCube);

            var showPlane = new Fluent.CheckBox { Header = "基准面", IsChecked = true };
            showPlane.Click += (s, e) => { Host.AxisPlaneGridModel3D.IsRendering = showPlane.IsChecked.Value; };
            showPlane.IsChecked = Host.AxisPlaneGridModel3D.IsRendering;
            group.Items.Add(showPlane);

            var showShadow = new Fluent.CheckBox { Header = "阴影效果", IsChecked = true };
            showShadow.Click += (s, e) => { Host.ShadowMap3D.IsRendering = showShadow.IsChecked.Value; };
            showShadow.IsChecked = Host.ShadowMap3D.IsRendering;
            group.Items.Add(showShadow);

            var showCamera = new Fluent.CheckBox { Header = "相机", IsChecked = true };
            showCamera.Click += (s, e) => { Host.ActiveView.ShowCameraInfo = showCamera.IsChecked.Value; };
            showCamera.IsChecked = Host.ActiveView.ShowCameraInfo;
            group.Items.Add(showCamera);

            var showFps = new Fluent.CheckBox { Header = "帧率", IsChecked = true };
            showFps.Click += (s, e) => { Host.ActiveView.ShowFrameRate = showFps.IsChecked.Value; };
            showFps.IsChecked = Host.ActiveView.ShowFrameRate;
            group.Items.Add(showFps);

            var showFrameDetails = new Fluent.CheckBox { Header = "帧率详情", IsChecked = true };
            showFrameDetails.Click += (s, e) => { Host.ActiveView.ShowFrameDetails = showFrameDetails.IsChecked.Value; };
            showFrameDetails.IsChecked = Host.ActiveView.ShowFrameDetails;
            group.Items.Add(showFrameDetails);

            var showTriangle = new Fluent.CheckBox { Header = "三角面片", IsChecked = true };
            showTriangle.Click += (s, e) => { Host.ActiveView.ShowTriangleCountInfo = showTriangle.IsChecked.Value; };
            showTriangle.IsChecked = Host.ActiveView.ShowTriangleCountInfo;
            group.Items.Add(showTriangle);
            _nodePanel = new NodePanel(Host);
            _nodes = new LayoutAnchorable { Title = "节点", Content = _nodePanel };
            Host.UI.LeftPanel.Children.Add(_nodes);

            _materialPanel = new MaterialPanel(Host);
            _materials = new LayoutAnchorable { Title = "材质", Content = _materialPanel };
            Host.UI.RightPanel.Children.Add(_materials);

            _animationPanel = new AnimationPanel(Host);
            _animations = new LayoutAnchorable { Title = "动画", Content = _animationPanel };
            Host.UI.RightPanel.Children.Add(_animations);

            _propertyPanel = new PropertyPanel(Host);
            _properties = new LayoutAnchorable { Title = "属性", Content = _propertyPanel };
            Host.UI.RightPanel.Children.Add(_properties);
        }
        public void OnEvent(string eventName, object data)
        {
            if (eventName == "SceneChanged")
            {
                _nodePanel.Update(Host.Model);
                _animationPanel.Update();
            }
            else if (eventName == "SelectedChanged")
            {
                _nodePanel.UpdateSelect();
                _materialPanel.Update();
                _propertyPanel.Update();
            }
        }

        public void Clearup()
        {
        }
    }
}